﻿#region DaTfiles
/*----------------------------------------------------------------
// 文件名：DaTfiles.cs
// 功能描述：表dbo.TFiles的数据访问层
//
// 
// 创建时间：2009-11-22 DtataAccess template . Ver 5.0.20090413
//
// 修改标识： 
// 修改描述： 
//----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Globalization;
using System.Text;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using EQT.Model;


namespace EQT.Dal
{
    ///可以继承更上层接口,ITfiles ,数据访问层基类DaBase，
    ///DaBase中包含了DbException异常处理静态方法
    public partial class DaTfiles : DaBase<MoTfiles>
    {
        #region SQL const
        internal const string TABLE_COLUMNS = " FILE_ID ,FILE_LENGTH ,FILE_URL ,FILE_NAME ,FILE_CREATE_TIME ,CREATOR_ID ,FREM ,CLASS1   ";

        internal const string SQL_INSERT = "INSERT INTO TFILES (" + TABLE_COLUMNS + ") VALUES (@FileId,@FileLength,@FileUrl,@FileName,getdate(),@CreatorId,@Frem,@Class1)";
        public const string SQL_SELECT = "SELECT " + TABLE_COLUMNS + " FROM TFILES  ";
        internal const string SQL_SELECT_ONE = SQL_SELECT + " WHERE FILE_ID=@FileId";

        internal const string SQL_EXIST = "SELECT COUNT(*) FROM TFILES  WHERE FILE_ID=@FileId  ";
        internal const string SQL_UPDATE = "UPDATE TFILES SET FILE_LENGTH=@FileLength, FILE_URL=@FileUrl, FILE_NAME=@FileName, FILE_CREATE_TIME=getdate(), CREATOR_ID=@CreatorId, FREM=@Frem, CLASS1=@Class1 WHERE FILE_ID=@FileId";

        internal const string SQL_DELETE_DEFAULT = "DELETE FROM TFILES ";
        internal const string SQL_DELETE = "DELETE FROM TFILES WHERE FILE_ID=@FileId";

        internal const string SQL_COUNT = "SELECT COUNT(*) FROM TFiles ";
        #endregion

        #region Constructor

        /// <summary>
        /// 默认构造函数，使用配置文件中默认的数据库配置。
        /// </summary>
        public DaTfiles()
        {
            this.db = DatabaseFactory.CreateDatabase();
        }
        /// <summary>
        /// 使用指定的databaseName数据库配置。
        /// </summary>
        /// <param name="databaseName">配置文件中数据库的配置名称。</param>
        public DaTfiles(string databaseName)
        {
            this.db = DatabaseFactory.CreateDatabase(databaseName);
        }

        public DaTfiles(Database db)
        {
            this.db = db;
        }

        #endregion

        //以下是CRUD方法，每个方法都有引入外部事务的版本

        #region 构造器模块

        internal override MoTfiles ConstructT()
        {
            return new MoTfiles();
        }
        #endregion



        #region Add模块 Helper

        ///build the command object.It never throw exception.
        internal override DbCommand ConstructAddCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);

            db.AddInParameter(dbCommand, "FileId", DbType.AnsiString);
            db.AddInParameter(dbCommand, "FileLength", DbType.Int64);
            db.AddInParameter(dbCommand, "FileUrl", DbType.String);
            db.AddInParameter(dbCommand, "FileName", DbType.String);
            //db.AddInParameter(dbCommand, "FileCreateTime", DbType.DateTime);
            db.AddInParameter(dbCommand, "CreatorId", DbType.String);
            db.AddInParameter(dbCommand, "Frem", DbType.String);
            db.AddInParameter(dbCommand, "Class1", DbType.String);
            return dbCommand;
        }
        internal override DbCommand ConstructAddCommand(Database db, MoTfiles entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);
            db.AddInParameter(dbCommand, "FileId", DbType.AnsiString, entity.FileId);
            db.AddInParameter(dbCommand, "FileLength", DbType.Int64, entity.FileLength);
            db.AddInParameter(dbCommand, "FileUrl", DbType.String, entity.FileUrl);
            db.AddInParameter(dbCommand, "FileName", DbType.String, entity.FileName);
            //db.AddInParameter(dbCommand, "FileCreateTime", DbType.DateTime, entity.FileCreateTime);
            db.AddInParameter(dbCommand, "CreatorId", DbType.String, entity.CreatorId);
            db.AddInParameter(dbCommand, "Frem", DbType.String, entity.Frem);
            db.AddInParameter(dbCommand, "Class1", DbType.String, entity.Class1);
            return dbCommand;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, MoTfiles entity)
        {
            db.SetParameterValue(addCmd, "FileId", entity.FileId);
            db.SetParameterValue(addCmd, "FileLength", entity.FileLength);
            db.SetParameterValue(addCmd, "FileUrl", entity.FileUrl);
            db.SetParameterValue(addCmd, "FileName", entity.FileName);
            //db.SetParameterValue(addCmd, "FileCreateTime", entity.FileCreateTime);
            db.SetParameterValue(addCmd, "CreatorId", entity.CreatorId);
            db.SetParameterValue(addCmd, "Frem", entity.Frem);
            db.SetParameterValue(addCmd, "Class1", entity.Class1);
            return addCmd;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, DataRow row)
        {
            db.SetParameterValue(addCmd, "FileId", row["FileId"]);
            db.SetParameterValue(addCmd, "FileLength", row["FileLength"]);
            db.SetParameterValue(addCmd, "FileUrl", row["FileUrl"]);
            db.SetParameterValue(addCmd, "FileName", row["FileName"]);
            //db.SetParameterValue(addCmd, "FileCreateTime", row["FileCreateTime"]);
            db.SetParameterValue(addCmd, "CreatorId", row["CreatorId"]);
            db.SetParameterValue(addCmd, "Frem", row["Frem"]);
            db.SetParameterValue(addCmd, "Class1", row["Class1"]);
            return addCmd;
        }

        #endregion

        #region Update模块 Helper

        internal override DbCommand ConstructUpdateCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "FileLength", DbType.Int64);
            db.AddInParameter(dbCommand, "FileUrl", DbType.String);
            db.AddInParameter(dbCommand, "FileName", DbType.String);
            //db.AddInParameter(dbCommand, "FileCreateTime", DbType.DateTime);
            db.AddInParameter(dbCommand, "CreatorId", DbType.String);
            db.AddInParameter(dbCommand, "Frem", DbType.String);
            db.AddInParameter(dbCommand, "Class1", DbType.String);
            db.AddInParameter(dbCommand, "FileId", DbType.AnsiString);

            return dbCommand;
        }
        internal override DbCommand ConstructUpdateCommand(Database db, MoTfiles entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "FileLength", DbType.Int64, entity.FileLength);
            db.AddInParameter(dbCommand, "FileUrl", DbType.String, entity.FileUrl);
            db.AddInParameter(dbCommand, "FileName", DbType.String, entity.FileName);
            //db.AddInParameter(dbCommand, "FileCreateTime", DbType.DateTime, entity.FileCreateTime);
            db.AddInParameter(dbCommand, "CreatorId", DbType.String, entity.CreatorId);
            db.AddInParameter(dbCommand, "Frem", DbType.String, entity.Frem);
            db.AddInParameter(dbCommand, "Class1", DbType.String, entity.Class1);
            db.AddInParameter(dbCommand, "FileId", DbType.AnsiString, entity.FileId);

            return dbCommand;
        }

        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, MoTfiles entity)
        {
            db.SetParameterValue(updateCmd, "FileLength", entity.FileLength);
            db.SetParameterValue(updateCmd, "FileUrl", entity.FileUrl);
            db.SetParameterValue(updateCmd, "FileName", entity.FileName);
            //db.SetParameterValue(updateCmd, "FileCreateTime", entity.FileCreateTime);
            db.SetParameterValue(updateCmd, "CreatorId", entity.CreatorId);
            db.SetParameterValue(updateCmd, "Frem", entity.Frem);
            db.SetParameterValue(updateCmd, "Class1", entity.Class1);
            db.SetParameterValue(updateCmd, "FileId", entity.FileId);
            return updateCmd;
        }
        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, DataRow row)
        {
            db.SetParameterValue(updateCmd, "FileLength", row["FileLength"]);
            db.SetParameterValue(updateCmd, "FileUrl", row["FileUrl"]);
            db.SetParameterValue(updateCmd, "FileName", row["FileName"]);
            //db.SetParameterValue(updateCmd, "FileCreateTime", row["FileCreateTime"]);
            db.SetParameterValue(updateCmd, "CreatorId", row["CreatorId"]);
            db.SetParameterValue(updateCmd, "Frem", row["Frem"]);
            db.SetParameterValue(updateCmd, "Class1", row["Class1"]);
            db.SetParameterValue(updateCmd, "FileId", row["FileId"]);
            return updateCmd;
        }

        internal override void PrepareDataAdapterCommand(Database db, out DbCommand dbInsertCommand,
                             out DbCommand dbUpdateCommand, out DbCommand dbDeleteCommand)
        {
            dbInsertCommand = db.GetSqlStringCommand(SQL_INSERT);
            #region set insert cmd parameters
            db.AddInParameter(dbInsertCommand, "FileId", DbType.AnsiString, "FILE_ID", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "FileLength", DbType.Int64, "FILE_LENGTH", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "FileUrl", DbType.String, "FILE_URL", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "FileName", DbType.String, "FILE_NAME", DataRowVersion.Current);
            //db.AddInParameter(dbInsertCommand, "FileCreateTime", DbType.DateTime, "FILE_CREATE_TIME", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "CreatorId", DbType.String, "CREATOR_ID", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Frem", DbType.String, "FREM", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Class1", DbType.String, "CLASS1", DataRowVersion.Current);
            #endregion
            dbUpdateCommand = db.GetSqlStringCommand(SQL_UPDATE);
            #region Set update cmd value parameters
            db.AddInParameter(dbUpdateCommand, "FileLength", DbType.Int64, "FILE_LENGTH", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "FileUrl", DbType.String, "FILE_URL", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "FileName", DbType.String, "FILE_NAME", DataRowVersion.Current);
            //db.AddInParameter(dbUpdateCommand, "FileCreateTime", DbType.DateTime, "FILE_CREATE_TIME", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "CreatorId", DbType.String, "CREATOR_ID", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "Frem", DbType.String, "FREM", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "Class1", DbType.String, "CLASS1", DataRowVersion.Current);
            #endregion
            #region set update cmd pk where parameters
            db.AddInParameter(dbUpdateCommand, "FileId", DbType.AnsiString, "FILE_ID", DataRowVersion.Current);
            #endregion
            dbDeleteCommand = db.GetSqlStringCommand(SQL_DELETE);
            #region set delete cmd pk where parameters
            db.AddInParameter(dbDeleteCommand, "FileId", DbType.AnsiString, "FILE_ID", DataRowVersion.Current);
            #endregion
        }
        #endregion

        #region Delete模块 Helper

        internal override DbCommand ConstructDeleteCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "FileId", DbType.AnsiString);

            return dbCommand;
        }
        internal override DbCommand ConstructDeleteCommand(Database db, MoTfiles entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "FileId", DbType.AnsiString, entity.FileId);

            return dbCommand;
        }

        internal override DbCommand ConstructDeleteCommandForWhere(Database db, string where)
        {
            return db.GetSqlStringCommand(SQL_DELETE_DEFAULT + where);
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, MoTfiles entity)
        {
            db.SetParameterValue(deleteCmd, "FileId", entity.FileId);
            return deleteCmd;
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, DataRow row)
        {
            db.SetParameterValue(deleteCmd, "FileId", row["FileId"]);
            return deleteCmd;
        }

        #endregion helper

        #region Query模块 Helper

        internal override DbCommand ConstructQueryCommand(string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region GetEntity(s)模块 Helper

        internal override DbCommand ConstructSelectOneCommand(Database db, MoTfiles entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_SELECT_ONE);

            db.AddInParameter(dbCommand, "FileId", DbType.AnsiString, entity.FileId);
            return dbCommand;
        }

        internal override void PopulateEntityByDataReader(IDataReader reader, ref MoTfiles entity)
        {
            if (!reader.IsDBNull(0)) entity.FileId = reader.GetString(0);
            if (!reader.IsDBNull(1)) entity.FileLength = reader.GetInt64(1);
            if (!reader.IsDBNull(2)) entity.FileUrl = reader.GetString(2);
            if (!reader.IsDBNull(3)) entity.FileName = reader.GetString(3);
            if (!reader.IsDBNull(4)) entity.FileCreateTime = reader.GetDateTime(4);
            if (!reader.IsDBNull(5)) entity.CreatorId = reader.GetString(5);
            if (!reader.IsDBNull(6)) entity.Frem = reader.GetString(6);
            if (!reader.IsDBNull(7)) entity.Class1 = reader.GetString(7);
        }



        internal override DbCommand ConstructSelectConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region Count模块 Helper

        internal override DbCommand ConstructCountCommand(Database db)
        {
            return db.GetSqlStringCommand(SQL_COUNT);
        }

        internal override DbCommand ConstructCountConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_COUNT + condition);
        }

        #endregion

        #region IsExist模块 Helper

        internal override DbCommand ConstructIsExistCommand(Database db, MoTfiles entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_EXIST);
            db.AddInParameter(dbCommand, "FileId", DbType.AnsiString, entity.FileId);
            return dbCommand;
        }

        #endregion



    }
}
#endregion

